package com.yu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.constant.JwtClaimsConstant;
import com.yu.mapper.DietMapper;
import com.yu.mapper.DishMapper;
import com.yu.mapper.ExerciseMapper;
import com.yu.mapper.PatientMapper;
import com.yu.model.Result;
import com.yu.model.dto.PatientDTO;
import com.yu.model.dto.PatientPageDTO;
import com.yu.model.entity.Diet;
import com.yu.model.entity.Dish;
import com.yu.model.entity.Exercise;
import com.yu.model.entity.Patient;
import com.yu.model.vo.PatientPlan;
import com.yu.properties.JwtProperties;
import com.yu.service.PatientService;
import com.yu.utils.JwtUtil;
import com.yu.utils.PatientHolder;
import com.yu.utils.StatusHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 33522
 * @description 针对表【patient】的数据库操作Service实现
 * @createDate 2023-12-18 10:52:34
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>
        implements PatientService {


    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private DietMapper dietMapper;


    @Resource
    private ExerciseMapper exerciseMapper;

    @Override
    public void modifyPatient(Patient patient) {
        updateById(patient);

    }

    @Override
    public Page<Patient> pageList(PatientPageDTO patientPageDTO) {
        Page<Patient> patientPage = new Page<>(patientPageDTO.getPage(), patientPageDTO.getPageSize());
        LambdaQueryWrapper<Patient> patientLambdaQueryWrapper = new LambdaQueryWrapper<>();
        patientLambdaQueryWrapper.eq(patientPageDTO.getNickName() != null, Patient::getNickName, patientPageDTO.getNickName());
        patientLambdaQueryWrapper.eq(patientPageDTO.getDoctorId() != null, Patient::getDoctorId, patientPageDTO.getDoctorId());
        page(patientPage, patientLambdaQueryWrapper);
        return patientPage;
    }

    @Override
    public Result<String> login(PatientDTO patientDTO) {
        QueryChainWrapper<Patient> phone = query().eq("phone", patientDTO.getPhone());
        Patient one = getOne(phone);
        if (one == null) {
            return Result.error("用户不存在");
        }

        String password = DigestUtils.md5DigestAsHex(patientDTO.getPassword().getBytes());
        if (!password.equals(one.getPassword())) {
            return Result.error("账号或密码不正确");
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.PAI_ID, one.getId());
        claims.put("status", "doctor");
        String jwt = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        StatusHolder.saveStatus("patient");
        PatientHolder.saveUser(one);
        return Result.success(jwt);
    }

    @Override
    public PatientPlan listExercise() {
        PatientPlan patientPlan = new PatientPlan();
        Patient patient = PatientHolder.getPatient();
        LambdaQueryWrapper<Exercise> exerciseLambdaQueryWrapper = new LambdaQueryWrapper<>();

        exerciseLambdaQueryWrapper.eq(Exercise::getPatientId, patient.getId());
        exerciseLambdaQueryWrapper.eq(Exercise::getDoctorId, patient.getDoctorId());
        List<Exercise> exercises = exerciseMapper.selectList(exerciseLambdaQueryWrapper);

        LambdaQueryWrapper<Diet> dietLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dietLambdaQueryWrapper.eq(Diet::getPatientId, patient.getId()).eq(Diet::getDoctorId, patient.getDoctorId());
        List<Diet> diets = dietMapper.selectList(dietLambdaQueryWrapper);
        List<Dish> dishList = diets.stream().map((item) -> {
            return dishMapper.selectById(item.getDishId());
        }).collect(Collectors.toList());
        patientPlan.setExercise(exercises);
        patientPlan.setDish(dishList);

        return patientPlan;
    }
}




